﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLLopHoc.DoiTuongDuLieu;
using System.Data;

namespace QLLopHoc.KetNoiDatabase
{
    public class KNGiangVien
    {
        public static DTGiangVien LayGVTuMS(string msgv)
        {
            DTGiangVien kq = null;
            string sql = "select * from GiangVien where MSGV='" + msgv + "'";
            DataTable dt = DataAccess.execquery(sql);
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                kq = new DTGiangVien();
                kq.MSGV = (string)dr["MSGV"];
                kq.Hoten = (string)dr["HoTen"];
                kq.Email = (string)dr["Email"];
                kq.Tel = (string)dr["Tel"];
            }
            return kq;
        }
        public static bool ThemGV(DTGiangVien gv)
        {
            string sql = "INSERT INTO GiangVien(MSGV,HoTen,Email,Tel) values('" + gv.MSGV + "','" + gv.Hoten + "','" + gv.Email + "','" + gv.Tel + "')";
            //string sql = "INSERT INTO GiangVien(MSGV,HoTen,Email,Tel) VALUES('" + gv.MSGV + "','" + gv.HoTen + "','" + gv.Email + "','" + gv.Tel + "')";
            if (DataAccess.ExecNonQuery(sql) > 0)
            {
                return true;
            }
            return false;
        }
        //Phuong thuc xoa 1 giang vien
        public static bool XoaGV(string msgv)
        {
            string sql = "DELETE from GiangVien where MSGV='" + msgv + "'";
            if (DataAccess.ExecNonQuery(sql) > 0)
            {
                return true;
            }
            return false;
        }
        public static bool CapNhatGiangVien(DTGiangVien gv)
        {
            string sql = "update GiangVien set HoTen='" + gv.Hoten + "',Email='" + gv.Email + "',Tel='" + gv.Tel + "',where MSGV='" + gv.MSGV + "'";
            if (DataAccess.ExecNonQuery(sql) > 0)
            {
                return true;
            }
            return false;
        }

        //Phương thức lấy danh sách giảng viên 
        public static List<DTGiangVien> LayDSGV()
        {
            List<DTGiangVien> dsgv = new List<DTGiangVien>();
            string sql = "SELECT * FROM GiangVien";
            DataTable dt = DataAccess.execquery(sql);
            DTGiangVien gv = null;
            foreach (DataRow dr in dt.Rows)
            {
                gv = new DTGiangVien();
                gv.MSGV = (string)dr["MSGV"];
                gv.Hoten = (string)dr["HoTen"];
                gv.Email = (string)dr["Email"];
                gv.Tel = (string)dr["Tel"];
                dsgv.Add(gv);
            }
            return dsgv;
        } 
    }
}
